package com.jphoebe.plugin.mybatis.prodvider;

import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.intellij.codeInsight.daemon.RelatedItemLineMarkerInfo;
import com.intellij.codeInsight.daemon.RelatedItemLineMarkerProvider;
import com.intellij.codeInsight.navigation.NavigationGutterIconBuilder;
import com.intellij.openapi.editor.markup.GutterIconRenderer;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiNameIdentifierOwner;
import com.intellij.psi.xml.XmlTag;
import com.intellij.util.CommonProcessors;
import com.intellij.util.xml.DomElement;
import com.jphoebe.plugin.mybatis.bean.setting.GlobalSetting;
import com.jphoebe.plugin.mybatis.dom.attribute.IdDomElement;
import com.jphoebe.plugin.mybatis.service.JavaService;
import com.jphoebe.plugin.mybatis.service.setting.GlobalSettingService;
import com.jphoebe.plugin.mybatis.utils.Il8nUtil;
import com.jphoebe.plugin.mybatis.utils.PropertiesUtil;
import com.jphoebe.plugin.mybatis.utils.jetbrain.IconsUtil;
import com.jphoebe.plugin.mybatis.utils.jetbrain.JetbrainJavaUtil;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Objects;

/**
 * java 接口类 跳转到 mapper xml文件
 * java mapper interface jump to mapper xml file
 *
 * @author 蒋时华
 * @date 2019/2/18
 */
public class Java2MapperLineMarkerProvider extends RelatedItemLineMarkerProvider {

    @Override
    protected void collectNavigationMarkers(@NotNull PsiElement element, @NotNull Collection<? super RelatedItemLineMarkerInfo> result) {
        GlobalSetting globalSetting = GlobalSettingService.getInstance().getState();
        if (!globalSetting.getNaviJumpStatus()){
            return ;
        }
        //如果element是PsiNameIdentifierOwner对象，且是接口
        if (element instanceof PsiNameIdentifierOwner && JetbrainJavaUtil.isElementWithinInterface(element)) {
            //表示ID元素的一个集合列表
            CommonProcessors.CollectProcessor<IdDomElement> processor = new CommonProcessors.CollectProcessor<>();
            JavaService.getInstance(element.getProject()).process(element, processor);
            Collection<IdDomElement> results = processor.getResults();
            if (!results.isEmpty()) {
                // 创建指向mapper xml导航图标
                NavigationGutterIconBuilder<PsiElement> builder =
                        NavigationGutterIconBuilder.create(IconsUtil.getJavaMapperIcon(globalSetting.getNaviJumpIcon()).getTo())
                                .setAlignment(GutterIconRenderer.Alignment.CENTER)
                                .setTargets(Collections2.transform(results, new Function<DomElement, XmlTag>() {
                                    @Override
                                    public XmlTag apply(DomElement domElement) {
                                        return domElement.getXmlTag();
                                    }
                                }))
                                .setTooltipTitle(Il8nUtil.getCurrnetSysProp(globalSetting.getLanguage()).getStr("Navigation.toMapper.tip"));
                result.add(builder.createLineMarkerInfo(Objects.requireNonNull(((PsiNameIdentifierOwner) element).getNameIdentifier())));
            }
        }
    }
}
